package org.light.verb;

import java.util.ArrayList;
import java.util.List;

import org.light.core.Verb;
import org.light.core.Writeable;
import org.light.domain.Domain;
import org.light.domain.JavascriptBlock;
import org.light.domain.JavascriptMethod;
import org.light.domain.Method;
import org.light.domain.Signature;
import org.light.domain.Statement;
import org.light.domain.Type;
import org.light.easyui.EasyUIPositions;
import org.light.exception.ValidateException;
import org.light.generator.NamedStatementGenerator;
import org.light.generator.NamedStatementListGenerator;
import org.light.utils.InterVarUtil;
import org.light.utils.StringUtil;
import org.light.utils.WriteableUtil;

public class SearchIndexedNamesByShortName extends Verb implements EasyUIPositions {

	@Override
	public Method generateDaoImplMethod() throws Exception {
		if (this.denied)
			return null;
		else {
			Method method = new Method();
			method.setStandardName("search" +this.domain.getCapFirstDomainName() + "IndexedNamesByShortName");
			method.setReturnType(new Type("List",this.domain, this.domain.getPackageToken()));
			method.addAdditionalImport("java.sql.Connection");
			method.addAdditionalImport("java.util.List");
			method.addAdditionalImport(this.domain.getPackageToken()+"."+ this.domain.getDomainSuffix() +"."+this.domain.getCapFirstDomainNameWithSuffix());
			method.addSignature(new Signature(1,"connection",new Type("Connection","java.sql"),"java.sql"));
			method.addSignature(new Signature(2,"shortName",new Type("String")));
			method.setThrowException(true);
			
			List<Writeable> list = new ArrayList<Writeable>();
			list.add(NamedStatementGenerator.getTryHead(50L,2));
			list.add(NamedStatementGenerator.getSearchIndexedNamesByShortNameStatement(100L,3,domain,InterVarUtil.DB.query));
			list.add(NamedStatementGenerator.getPrepareStatement(200L,3,InterVarUtil.DB.ps, InterVarUtil.DB.query, InterVarUtil.DB.connection));
			list.add(new Statement(300L,3,StringUtil.lowerFirst(InterVarUtil.DB.ps.getVarName())+"."+Type.getPrimeTypeSetterName("String")+"(1,shortName);"));
			list.add(NamedStatementGenerator.getPrepareStatementExcuteQuery(400L,3, InterVarUtil.DB.result, InterVarUtil.DB.ps));
			list.add(NamedStatementGenerator.getPrepareDaomainArrayList(500L,3, domain));			
			list.add(new Statement(600L, 3,"if (result.next()) {")); 
			list.add(NamedStatementGenerator.getPrepareDomainVarInit(700L,4, domain));
			list.add(NamedStatementGenerator.getSingleLineComment(750L,4,"Build the object."));
			list.add(NamedStatementListGenerator.generateSetDomainDataFromResultSet(800L,4,domain, InterVarUtil.DB.result));
			list.add(NamedStatementGenerator.getAddDomaintoList(850L,4, domain, InterVarUtil.Container.getList(domain)));
			list.add(new Statement(1000L, 3,"}"));
			list.add(NamedStatementGenerator.getReturnDomainList(1100,4, domain, InterVarUtil.Container.getList(domain)));
			list.add(NamedStatementListGenerator.generateCatchExceptionPrintStackReturnNullFooter(1200L,2));
			method.setMethodStatementList(WriteableUtil.merge(list));
			return method;
		}
	}

	@Override
	public Method generateDaoMethodDefinition() throws Exception {
		if (this.denied)
			return null;
		else {
			Method method = new Method();
			method.setStandardName("search" +this.domain.getCapFirstDomainName() + "IndexedNamesByShortName");
			method.setReturnType(new Type("List", this.domain, this.domain.getPackageToken()));
			method.addAdditionalImport("java.util.List");
			method.addAdditionalImport(this.domain.getPackageToken() + "." + this.domain.getDomainSuffix() + "."
					+ this.domain.getCapFirstDomainNameWithSuffix());
			method.addSignature(new Signature(1,"connection",new Type("Connection","java.sql"),"java.sql"));
			method.addSignature(new Signature(2, "shortName", new Type("String")));
			method.setThrowException(true);
			return method;
		}
	}

	@Override
	public Method generateServiceMethodDefinition() throws Exception {
		return null;
	}

	@Override
	public Method generateServiceImplMethod() throws Exception {
		return null;
	}

	public SearchIndexedNamesByShortName() {
		super();
		this.dbType = "MariaDB";
		this.setLabel("按短名搜索");
	}

	public SearchIndexedNamesByShortName(Domain domain)  throws ValidateException{
		super();
		this.domain = domain;
		this.dbType = "MariaDB";
		this.denied = domain.isVerbDenied("Clone");
		this.setVerbName("Search" + this.domain.getCapFirstDomainName() + "IndexedNamesByShortName");
		this.setLabel("按短名搜索");
		if  (domain.getLanguage().equalsIgnoreCase("english"))  this.setLabel("SearchIndexedNamesByShortName");
	}
	
	public SearchIndexedNamesByShortName(Domain domain, String dbType)  throws ValidateException{
		super();
		this.domain = domain;
		this.dbType = dbType;
		this.denied = domain.isVerbDenied("SearchByName");
		this.setVerbName("Search" + this.domain.getCapFirstDomainName() + "IndexedNamesByShortName");
		this.setLabel("按名字搜索");
		if  (domain.getLanguage().equalsIgnoreCase("english"))  this.setLabel("SearchByName");
	}

	@Override
	public JavascriptBlock generateEasyUIJSButtonBlock() throws Exception {
		return null;
	}

	@Override
	public JavascriptMethod generateEasyUIJSActionMethod() throws Exception {
		return null;
	}

	@Override
	public Method generateDummyDaoImplMethod() throws Exception {
		if (this.denied)
			return null;
		else {
			Method method = new Method();
			method.setStandardName("search" +this.domain.getCapFirstDomainName() + "IndexedNamesByShortName");
			method.setReturnType(new Type("List", this.domain, this.domain.getPackageToken()));
			method.addAdditionalImport("java.util.List");
			method.addAdditionalImport(this.domain.getPackageToken() + "." + this.domain.getDomainSuffix() + "."
					+ this.domain.getCapFirstDomainNameWithSuffix());
			method.addSignature(new Signature(1, "shortName", new Type("String")));
			method.setThrowException(true);

			List<Writeable> sList = new ArrayList<Writeable>();
			sList.add(new Statement(1000L,2,"List<"+this.domain.getCapFirstDomainNameWithSuffix()+"> results = new ArrayList<>();"));
			sList.add(new Statement(2000L,2,"for ("+this.domain.getCapFirstDomainNameWithSuffix()+" "+this.domain.getLowerFirstDomainName()+" : db) {"));
			sList.add(new Statement(3000L,3,"if ("+this.domain.getLowerFirstDomainName()+".get"+this.domain.getDomainName().getCapFirstFieldName()+"().startsWith(shortName)) results.add("+this.domain.getLowerFirstDomainName()+");"));
			sList.add(new Statement(4000L,2,"}"));
			sList.add(new Statement(5000L,2,"return results;"));
			method.setMethodStatementList(WriteableUtil.merge(sList));
			return method;
		}
	}

	@Override
	public Method generateControllerMethod() throws Exception {
		return null;
	}

	@Override
	public Method generateDummyServiceImplMethod() throws Exception {
		return null;
	}

	@Override
	public Method generateDummyDaoMethodDefinition() throws Exception {
		if (this.denied)
			return null;
		else {
			Method method = new Method();
			method.setStandardName("search" +this.domain.getCapFirstDomainName() + "IndexedNamesByShortName");
			method.setReturnType(new Type("List", this.domain, this.domain.getPackageToken()));
			method.addAdditionalImport("java.util.List");
			method.addAdditionalImport(this.domain.getPackageToken() + "." + this.domain.getDomainSuffix() + "."
					+ this.domain.getCapFirstDomainNameWithSuffix());
			method.addSignature(new Signature(1, "shortName", new Type("String")));
			method.setThrowException(true);
			return method;
		}
	}
}
